//
//  KDRuntimeTest.m
//  demo
//
//  Created by 叶青 on 15/8/22.
//  Copyright (c) 2015年 naruto. All rights reserved.
//

#import "KDRuntimeTest.h"
#import <objc/runtime.h>

@implementation KDRuntimeTest
#pragma mark - system
+ (void)initialize
{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        Class class = object_getClass(NSClassFromString(@"KDRuntimeTest"));
//        class_addMethod(class, @selector(kdtest1), (IMP) dynamicMethodIMP, "@:@");
//    });
}


#pragma mark - public

/**
 *  @brief  测试方法
 *
 *  @return void
 */
+ (void)kdtest
{
    NSLog(@"KDRuntimeTest %@",NSStringFromSelector(_cmd));
}

- (void)kdtest
{
    NSLog(@"KDRuntimeTest obj %@",NSStringFromSelector(_cmd));
}


#pragma mark - class cluster

/**
 *  类簇测试
 */
-(void)testClassCluster
{
    //    id aNumTmp = [NSNumber alloc];
    //    NSLog(@"aNumTmp class:%@", [aNumTmp class]);//NSPlaceholderNumber
    //    NSNumber *aChar = [aNumTmp initWithChar:'a'];
    //    NSLog(@"aChar class:%@", [aChar class]);//__NSCFNumber
    //    NSNumber *aInt = [aNumTmp initWithInt:1];
    //    NSLog(@"aInt class:%@", [aInt class]);//__NSCFNumber
    //    NSNumber *aFloat = [aNumTmp initWithFloat:1.0];
    //    NSLog(@"aFloat class:%@", [aFloat class]);//__NSCFNumber
    //    NSNumber *aBool = [aNumTmp initWithBool:YES];
    //    NSLog(@"aBool class:%@", [aBool class]);//__NSCFBoolean
    //    NSNumber *aDouble = [aNumTmp initWithDouble:1.0];
    //    NSLog(@"aDouble class:%@", [aDouble class]);//__NSCFNumber
    //
    //    id aArrTmp = [NSArray alloc];
    //    NSLog(@"aArrTmp class:%@", [aArrTmp class]);//__NSPlaceholderArray
    //    NSArray *aArr = [aArrTmp initWithObjects:@"foo",@"bar", nil];
    //    NSLog(@"aArr class:%@", [aArr class]);//__NSArrayI
    //
    //    id aArrmTmp = [NSMutableArray alloc];
    //    NSLog(@"aArrmTmp class:%@", [aArrmTmp class]);//__NSPlaceholderArray
    //    NSArray *aArrm = [aArrmTmp initWithObjects:@"foo",@"bar", nil];
    //    NSLog(@"aArrm class:%@", [aArrm class]);//__NSArrayM

}

#pragma mark - reslove method
+(BOOL)resolveClassMethod:(SEL)sel
{
    NSLog(@"%@ %@",NSStringFromSelector(_cmd),NSStringFromSelector(sel));
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wundeclared-selector"
    if (sel == @selector(kdtestClassMethod)){
        class_addMethod(object_getClass([KDRuntimeTest class]), sel, (IMP) dynamicMethodIMP, "@:@");
        return YES;
    }
#pragma clang diagnostic pop
    return [super resolveClassMethod:sel];
}
+(BOOL)resolveInstanceMethod:(SEL)sel
{
    NSLog(@"%@ %@",NSStringFromSelector(_cmd),NSStringFromSelector(sel));
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wundeclared-selector"
    if (sel == @selector(kdtestObjectMethod)){
        class_addMethod([self class], sel, (IMP) dynamicMethodIMP, "v@:");
        return NO;
    }
#pragma clang diagnostic pop
    return [super resolveInstanceMethod:sel];
}


void dynamicMethodIMP(id self, SEL _cmd)
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
}


//#pragma mark - forward
//-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
//{
//    NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
//    if (signature == nil) {
//        signature = [KDRuntimeTest methodSignatureForSelector:aSelector];
//    }
//    return signature;
//}
//-(void)forwardInvocation:(NSInvocation *)anInvocation
//{
//    if([KDRuntimeTest respondsToSelector:[anInvocation selector]]){
//        [anInvocation invokeWithTarget:[KDRuntimeTest class]];
//    }
//    else{
//        [super forwardInvocation:anInvocation];
//    }
//}
//-(void)doesNotRecognizeSelector:(SEL)aSelector
//{
//    NSLog(@"aSelector:%@",NSStringFromSelector(aSelector));
//}

@end
